package com.DefectTracking;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;


public class SendMail {
    
    	public SendMail() {
    	    
    	}
    	
    	public void SendMainToAssignee(String mail_to, String mail_summary, String mail_details  ) {
    	    
    	    	//Address of mailto and mailfrom
    	    	String mailFrom = "defecttrackingsystemucijava2@gmail.com";    	    	
    	    	String mailTo = mail_to ; 
    	  
	
    	    	//Setting the mail title and contents
    	    	String title = "New/Update the defect status<" + mail_summary +">";
    	    	String contents = "This mail is sended by Defect Tracking System<br> Please check the defect detail<br>" +
			  "and check a system ASAP<br>" +
			  "<br>-----------------------------details are---------------------------------<br><pre> " +
			  mail_details +
			  "</pre><br>-------------------------------------------------------------------------";


    	    	// To create Session setting the smtp information
    	    	Properties props = new Properties();
    	    	props.put("mail.smtp.host", "smtp.gmail.com");
    	    	props.put("mail.smtp.port", "587");
    	    	props.put("mail.smtp.starttls.enable","true");
    	    	props.put("mail.smtp.auth", "true");
    	    	props.put("mail.smtp.user", "defecttrackingsystemucijava2@gmail.com");
    	    	props.put("mail.smtp.debug", "true");

    	    	//Create session and send mail
    	    	try {	    
    	    	    	//Create Session with password authentication//using real gmail account/password 
    	    	    	Session sess = Session.getInstance(props, new Authenticator() { 
						protected PasswordAuthentication getPasswordAuthentication() { 
			    				return new PasswordAuthentication("defecttrackingsystemucijava2@gmail.com", "ucijava2");
						}
    	    	    			});
		
    	    	    	//enable session debug
    	    	    	sess.setDebug(true);		
		
    	    	    	//Create MimeMessages
    	    	    	MimeMessage msg = new MimeMessage(sess);		 
		 
    	    	    	InternetAddress from = new InternetAddress(mailFrom);
    	    	    	msg.setFrom(from);
    	    	    	InternetAddress to = new InternetAddress(mailTo);
    	    	    	msg.setSubject(title);
    	    	    	msg.setContent(contents, "text/html; charset=UTF-8");
    	    	    	msg.setRecipient(Message.RecipientType.TO, to);	 
	 	
    	    	    	Transport.send(msg);
    	    	    	System.out.println("Mail send Successfuly");
		
    	    	} catch (MessagingException e) {
    	    	    	System.out.println(e.getMessage());
    	    	    	System.out.println("Mail send failure");
    	    	}//try-catch
    
    	}//public SendMainToAssignee()
    
    
}// class
